<?php
class Mdev_checklist extends CI_Model {

    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    function get_outlet(){
      $this->db->where("id_status_outlet", 1);
      $this->db->or_where("id_status_outlet", 2);
      return $this->db->get("m_outlet")->result();
    }
    function get_user_auto($q){
      $q = strtolower($q);
      $query = "
        SELECT `B`.`id_users`, `B`.`name`, `C`.`name` as privilege
FROM `m_users` as B 
LEFT JOIN `d_user_privilege` as D ON `B`.`id_users` = `D`.`id_users`
LEFT JOIN `m_privilege` as C ON `D`.`id_privilege` = `C`.`id_privilege`
WHERE (LOWER(B.name)  LIKE '%{$q}%'
OR  LOWER(C.name)  LIKE '%{$q}%')
GROUP BY `B`.`id_users`
        ";
//print $query;
      $data = $this->db->query($query)->result();
      $items = array();
      if(is_array($data)){
        foreach($data as $dt){
          $items[$dt->name." | ".$dt->privilege] = $dt->id_users;
        }
      }
      return $items;
    }
    function tampil_isi($value, $no, $parent, $fase, $privilege){
    $link_edit = site_url("dev-checklist/add-new-keterangan/".$value->id_checklist_keterangan);
    $link_delete = site_url("dev-checklist/delete-keterangan/".$value->id_checklist_keterangan);

    if($value->cek == 1)
      $status = "<span class='label label-success'>Active</span>";
    else
      $status = "<span class='label label-important'>No Active</span>";

    $hasil = <<<EOD
    <tr>
      <td>{$no}</td>
      <td>{$value->name}</td>
      <td>{$fase}</td>
      <td>{$parent}</td>
      <td>{$privilege}</td>
      <td>{$value->sort}</td>
      <td>{$status}</td>
      <td>
        <div class="btn-group">
          <button data-toggle="dropdown" class="btn btn-small dropdown-toggle">Action<span class="caret"></span></button>
          <ul class="dropdown-menu">
            <li><a href="{$link_edit}">Edit</a></li>
            <li><a href="{$link_delete}">Delete</a></li>
          </ul>
        </div>
      </td>
    </tr>
EOD;
    return $hasil;
  }
  function get_auto_privilege($q){
      $q = strtolower($q);
      $query = "
        SELECT *
        FROM m_privilege
        WHERE 
        LOWER(name) LIKE '%{$q}%'
        ";
//print $query;
      $data = $this->db->query($query)->result();
      $items = array();
      if(is_array($data)){
        foreach($data as $dt){
          $items[$dt->name] = $dt->id_privilege;
        }
      }
      return $items;
    }
  function get_auto_level($fase, $q, $level1, $level2, $level3){
      $q = strtolower($q);
      $tanda = "level1";
      $w_level1 = "AND level1 > 0";
      $w_level2 = "AND level2 = 0";
      $w_level3 = "AND level3 = 0";
      $w_level4 = "AND level4 = 0";
      $w_level5 = "AND level5 = 0";
      $w_level6 = "AND level6 = 0";
      if($level1 > 0){
        $w_level1 = "AND level1 = {$level1}";
        $w_level2 = "AND level2 > 0";
        $tanda = "level2";
      }
      if($level2 > 0){
        $w_level1 = "AND level1 = {$level1}";
        $w_level2 = "AND level2 = {$level2}";
        $w_level3 = "AND level3 > 0";
        $tanda = "level3";
      }
      if($level3 > 0){
        $w_level1 = "AND level1 = {$level1}";
        $w_level2 = "AND level2 = {$level2}";
        $w_level3 = "AND level3 = {$level3}";
        $w_level4 = "AND level4 > 0";
        $tanda = "level4";
      }
      $query = "
        SELECT *
        FROM d_checklist_keterangan
        WHERE 
        id_checklist_fase = {$fase}
        $w_level1
        $w_level2
        $w_level3
        $w_level4
        $w_level5
        $w_level6
        AND LOWER(name) LIKE '%{$q}%'
        ";
//print $query;
      $data = $this->db->query($query)->result();
      
      $items = array();
      if(is_array($data)){
        foreach($data as $dt){
          $items[$dt->sort." ".$dt->name] = $dt->$tanda;
        }
      }
      return $items;
    }
    function get_milik($id = 0){
      $this->db->select("A.*");
      $this->db->join("d_outlet_users AS B", "A.id_outlet = B.id_outlet","left");
      if($this->nbscache->get_olahan("permission", $this->session->userdata("id_privilege"), "see_all_outlet", "add") !== FALSE AND $this->uri->segment(3) < 1){
        $id = 1;
      }
      if($id != 1){
        $this->db->where("B.id_users", $id);
       
      } 
      //$this->db->where("A.id_status_outlet", "2");
      $this->db->group_by("A.id_outlet");
      $this->db->order_by("A.id_status_outlet", "DESC"); 
      $data = $this->db->get("m_outlet AS A")->result();
      
      return $data;
    }
}
?>
